<template src="./template.html"></template>

<style src="./style.css" scoped></style>

<script>
     import defaultImg from 'assets/images/default-img.png';
     import shopIcon from 'assets/images/shop-ico.png';
     import leftLine from 'assets/images/line-left.png';
     import navigator from 'utils/navigator';
     import route from 'constants/route';
     import constant from 'constants/constant';
     import jsbridge from 'utils/jsbridge';
     import modal from 'utils/modal';
     import api from 'api/api';
     import notification from 'utils/notification';
     import cartService from 'services/cartService';

     /**解析读取的数据*/
     function vendorData(data, vm) {
        vm.goodsInfo = data || {};
        vm.imageList = data.img || [];
        vm.hotGoodsList = data.hotgoods || [];
    }

    export default {

        data () {
            return {
                pageLoaded: false,//页面加载完成
                defaultImg: defaultImg,
                shopIcon: shopIcon,
                leftLine: leftLine,
                indicatorIndex: 0, //图片轮播index
                goodsInfo: {}, //商品信息
                goodsId: null, //商品ID
                imageList: [],
                hotGoodsList: []
            }
        },
        created() {
            let bundleUrl = weex.config.bundleUrl;
            bundleUrl = bundleUrl.split("goodsId=")[1];
            if(!bundleUrl) {
                modal.toast("该商品不存在");
                return false;
            }
            this.goodsId = bundleUrl;
            this.get();

            //同步购物车商品数量
            cartService.resumeCartGoods(this);
        },
        methods: {
            /**获取数据*/
            get(callback) {
                let self = this;
                api.sendHttp('detailIndexApi', {
                    id: self.goodsId
                }).then(res => {
                    self.pageLoaded = true;
                    vendorData(res.data, self);
                }, res => {
                    self.pageLoaded = true;
                    modal.toast(res.msg || "获取商品详情失败");
                });
            },

            /**跳转到评价选项卡*/
            jumpToRateTab(e) {
                notification.send(constant.EVENT_TAB_CHANGE,2);
            },

            alarm(){ //显示抢购活动结束
                this.get();
            },

            /**跳转到店铺*/
            goToShop(){
                let self = this;
                if(self.goodsInfo.shoptype){ //大店铺shoptype
                    let _options = {
                        url: route.shop_index,
                        title: self.goodsInfo.s_cname || "商家店铺",
                        params: {
                            shopType: self.goodsInfo.shoptype
                        }
                    };
                    navigator.push(_options);
                } else {  //小店铺sid
                    let _options = {
                        url: route.shop_plus,
                        title: self.goodsInfo.s_cname || "店铺详情",
                        params: {
                            sid: self.goodsInfo.sid
                        }
                    };
                    navigator.push(_options);
                }
            },
            indicatorChange(e) {
                this.indicatorIndex = e.index;
            }
        },
        destroyed(){
            cartService.removeResumeCartGoodsListener();
        }
    }
</script>
